//AS3///////////////////////////////////////////////////////////////////////////
// 
// Copyright(c) 2009 180heartbeats. All rights reserved.
// Please read this carefully before using the source code.
// 
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions are
// met:
// 
// * Redistributions of source code must retain the above copyright notice, 
//   this list of conditions and the following disclaimer.
// 
// * Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the 
//   documentation and/or other materials provided with the distribution.
// 
// * Neither the name of 180heartbeats nor the names of its 
//   contributors may be used to endorse or promote products derived from 
//   this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
////////////////////////////////////////////////////////////////////////////////

package
{

import flash.display.Sprite;
import flash.events.Event;
import flash.utils.ByteArray;

import mx.utils.UIDUtil;

import com.heartbeats.crypto.cipher; 

[ SWF ( width = '320', height = '240', backgroundColor = '#FFFFFF', frameRate = '30' ) ]

/**
 * Class allow transforming information (referred to as plaintext) using an algorithm (called cipher) 
 * to make it unreadable to anyone except those possessing special knowledge, referred to as a key. 
 * The result of the process is encrypted information.
 * 
 * @langversion ActionScript 3.0
 * @playerversion Flash 10.0
 * 
 * @author Szymon Peplinski
 */
public class AlchemyCrypto extends Sprite
{
	/**
	 * @constructor
	 */
	public function AlchemyCrypto ()
	{
		super ();
		
		if ( this.stage )
		{
			this._initialize ();
		}
		else
		{
			this.addEventListener ( Event.ADDED_TO_STAGE, this._initialize )
		}
	}

	/**
	 * Initialize stub.
	 * 
	 * @param event The Event object that is dispatched into the event flow. 
	 * If the event is being redispatched, a clone of the event is created automatically. 
	 * After an event is dispatched, its target property cannot be changed, 
	 * so you must create a new copy of the event for redispatching to work.
	 */
	private function _initialize ( event : Event = null ) : void
	{
		this.stage.removeEventListener ( Event.ADDED_TO_STAGE, this._initialize );
		
		var input : String = "";
		
		// Adding some string.
		input = input + "zażółćgęśląjaźń" + ";";
		
		// Adding some integer.
		input = input + 123 + ";";
		
		// Adding some float.
		input = input + Math.PI + ";";
		
		// Adding unique id.
		input = input + UIDUtil.createUID () + ";";
		
		var output : String = this._encrypt ( input );
		
		throw Error ( "\nInput = " + input + "\nOutput = " + output );
	}
	
	/**
	  * Encrypts the data.
	  * 
	  * @param data The raw data.
	  * @param colons Colon hexadecimal notation.
	  * 
	  * @return Encrypted information.
	  */
	private function _encrypt ( data : String, colons : Boolean = false ) : String
	{
		var buffer : ByteArray = new ByteArray;
		buffer.writeUTFBytes ( data );
		
		var handler : uint = cipher.begin ();
		
		var encrypted : ByteArray = cipher.update ( handler, buffer );
		encrypted.writeBytes ( cipher.finish ( handler ) );
		
		return this._fromArray ( encrypted, colons );
	}
	
	/**
	 * Converts the byte array to a string.
	 * 
	 * @param array The byte stream.
	 * @param colons Colon hexadecimal notation.
	 * 
	 * @return The string representation of the byte array.
	 */
	private function _fromArray ( array : ByteArray, colons : Boolean = false ) : String 
	{
		var string : String = "";
		var i : int;
		
		for ( i = 0; i < array.length; i ++ ) 
		{
			string += ( "0" + array [ i ].toString ( 16 ) ).substr ( -2, 2 );
			
			if ( colons ) 
			{
				if ( i < array.length - 1 ) string += ":";
			}
		}
		
		return string;
	}
}

}
